package com.longrise.android.byjk.plugins.course.coursedetail.videoexercises;

import android.webkit.JavascriptInterface;

import com.longrise.android.byjk.event.CourseDetailEvent;
import com.longrise.common.base.RxManager;
import com.longrise.common.base.web.BaseWebActivity;
import com.longrise.common.base.web.BaseWebBridge;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by callxkj on 2017/10/30.
 * 课后练习题
 */

public class VideoExercisesBridge extends BaseWebBridge {

    public VideoExercisesBridge(BaseWebActivity activity, RxManager rxManager) {
        super(activity, rxManager);
    }

    /**
     * 交卷成功后,刷新课程目录
     */
    @JavascriptInterface
    public void updateCoursePraxis() {
        Disposable subscribe = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("11");
                e.onComplete();
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        //我的课程页面刷新数据还包括我的界面的刷新
                        CourseDetailEvent event = new CourseDetailEvent();
                        event.setUpdateCoursePraxis(true);
                        EventBus.getDefault().post(event);
                    }
                });
        mRxManager.addDisposable(subscribe);
    }
}
